package cn.yuanqiao.web.controller.archive.appManager;

import cn.yuanqiao.archive.appManager.domain.TenantFields;
import cn.yuanqiao.archive.appManager.domain.TenantMdataList;
import cn.yuanqiao.archive.appManager.service.ITenantMdataListService;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.web.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 元数据明细Controller
 * 
 * @author yuanqiao
 * @date 2023-06-09
 */
@RestController
@RequestMapping("/archive/mdataList")
public class TenantMdataListController extends BaseController
{
    @Autowired
    private ITenantMdataListService tenantMdataListService;

    /**
     * 查询元数据明细列表
     */
    //@PreAuthorize("@ss.hasPermi('manage:TenantMdataList:list')")
    @Log(title = "查询元数据明细列表", businessType = BusinessType.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(TenantMdataList tenantMdataList)
    {
        startPage();
        List<TenantMdataList> list = tenantMdataListService.selectTenantMdataListList(tenantMdataList);
        return getDataTable(list);
    }

    /**
     * 查询全部不带分页
     * @param tenantMdataList
     * @return
     */
    @GetMapping("/listAll")
    public AjaxResult listAll(TenantMdataList tenantMdataList)
    {
        List<TenantMdataList> list = tenantMdataListService.selectTenantMdataListList(tenantMdataList);
        return AjaxResult.success(list);
    }

    /**
     * 导出元数据明细列表
     */
    //@PreAuthorize("@ss.hasPermi('manage:TenantMdataList:export')")
    @Log(title = "元数据明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,  @RequestParam("groupId")Long groupId,@RequestParam("type")Integer type,@RequestParam(required = false,value = "fieldName")String fieldName,@RequestParam(required = false,value = "ids")List ids)
    {
        if(groupId == null){
            throw new ServiceException("参数错误");
        }
        List<TenantMdataList> list = tenantMdataListService.selectTenantMdataListListforexport(groupId,type,fieldName,ids);
        ExcelUtil<TenantMdataList> util = new ExcelUtil<TenantMdataList>(TenantMdataList.class);
        util.exportExcel(response, list, "元数据明细数据");
    }

    /**
     * 获取元数据明细详细信息
     */
    //@PreAuthorize("@ss.hasPermi('manage:TenantMdataList:query')")
    @Log(title = "获取元数据明细详细信息", businessType = BusinessType.SELECT)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tenantMdataListService.selectTenantMdataListById(id));
    }

    //@PreAuthorize("@ss.hasPermi('manage:TenantMdataList:add')")
    @Log(title = "元数据明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestParam String ids,@RequestParam Long groupId)
    {
        if(StringUtils.isEmpty(ids) || StringUtils.isNull(groupId)){
            return AjaxResult.error("请检查出入参数");
        }else {
            return toAjax(tenantMdataListService.insertTenantMdataList(ids,groupId));
        }
    }

    /**
     * 修改元数据明细
     */
    //@PreAuthorize("@ss.hasPermi('manage:TenantMdataList:edit')")
    @Log(title = "元数据明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TenantMdataList tenantMdataList)
    {
        if(StringUtils.isNull(tenantMdataList.getIds())){
            return AjaxResult.error("请检查输入参数！");
        }else {
            return toAjax(tenantMdataListService.updateTenantMdataList(tenantMdataList,tenantMdataList.getIds()));
        }
    }
    @Log(title = "元数据明细批量修改", businessType = BusinessType.UPDATE)
    @PutMapping("/batch")
    public AjaxResult batchEdit(@RequestBody TenantMdataList tenantMdataList)
    {
        if(StringUtils.isNull(tenantMdataList.getIds())){
            return AjaxResult.error("请检查输入参数！");
        }else {
            return toAjax(tenantMdataListService.batchUpdateTenantMdataList(tenantMdataList,tenantMdataList.getIds()));
        }
    }

    /**
     * 删除元数据明细
     */
    //@PreAuthorize("@ss.hasPermi('manage:TenantMdataList:remove')")
    @Log(title = "元数据明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tenantMdataListService.deleteTenantMdataListByIds(ids));
    }

    /**
     * 根据元数据ID查询基础数据
     *  @RequestParam Long groupId, @RequestParam String type, @RequestParam String fieldCaption, @RequestParam String fieldName
     */
    @Log(title = "根据元数据ID查询基础数据", businessType = BusinessType.SELECT)
    @PostMapping("/listByGroupId")
    public TableDataInfo getGroupDataList(@RequestParam Long groupId, @RequestParam String type, @RequestParam String fieldCaption, @RequestParam String fieldName){
        if(StringUtils.isNull(groupId) || StringUtils.isEmpty(type)){
            throw new ServiceException("参数错误！");
        }
        startPage();
        TenantFields yqfields = new TenantFields();
        yqfields.setType(type);
        yqfields.setFieldCaption(fieldCaption);
        yqfields.setFieldName(fieldName);
        List<TenantFields> getGroupDataList = tenantMdataListService.getGroupDataList(yqfields,groupId);
        return getDataTable(getGroupDataList);
    }

    /**
     * 查询已勾选元数据项列表
     * @param groupId
     * @return
     */
    @Log(title = "查询已勾选元数据项列表", businessType = BusinessType.SELECT)
    @GetMapping("/getHddaMDataListByIsMetadata")
    public AjaxResult getHddaMDataListByIsMetadata(@RequestParam Long groupId){
        if(StringUtils.isNull(groupId)){
            return AjaxResult.error("参数错误！");
        }
        TenantMdataList mDataList = new TenantMdataList();
        mDataList.setGroupId(groupId);
        mDataList.setIsMetadata(1);
        return AjaxResult.success(tenantMdataListService.selectTenantMdataListList(mDataList));
    }

    /**
     * 复制元数据项验证验证复制到的元数据方案中是否有重复
     * @param groupId
     * @return
     */
    @Log(title = "元数据方案中是否有重复", businessType = BusinessType.SELECT)
    @GetMapping("/copyIsRepeat")
    public AjaxResult copyIsRepeat(@RequestParam String ids,@RequestParam Long groupId,@RequestParam Integer type){
        if(StringUtils.isEmpty(ids) || StringUtils.isNull(groupId) || StringUtils.isNull(type)){
            return AjaxResult.error("参数错误！");
        }
        String msg = tenantMdataListService.copyIsRepeat(ids,groupId,type);
        if(StringUtils.isEmpty(msg)){
            return AjaxResult.success("200");
        }else {
            return AjaxResult.success("重复项有："+msg+"，请选择覆盖或者跳过？");
        }
    }

    /**
     * 复制到
     * type = 1 覆盖原来明细 type = 2 跳过
     * @return
     */
    @Log(title = "复制到元数据方案中", businessType = BusinessType.INSERT)
    @PostMapping("/copyToGroup")
    public AjaxResult copyToGroup(@RequestParam String ids,@RequestParam Long groupId,@RequestParam String type){
        if(StringUtils.isEmpty(ids) || StringUtils.isNull(groupId)){
            return AjaxResult.error("参数错误！");
        }
        tenantMdataListService.copyToGroup(ids,groupId,StringUtils.isNotEmpty(type) ? Integer.valueOf(type):null);
        return AjaxResult.success();
    }

    /**
     * 元数据明细导入
     * @param file
     * @param groupId
     * @param type
     * @return
     * @throws Exception
     */
    @Log(title = "元数据明细导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file,Long groupId,Integer type) throws Exception
    {
        if(file.isEmpty() || StringUtils.isNull(groupId) || StringUtils.isNull(type)){
            return AjaxResult.error("参数错误！");
        }
        ExcelUtil<TenantMdataList> util = new ExcelUtil<>(TenantMdataList.class);
        List<TenantMdataList> dataLists = util.importExcel(file.getInputStream());
        tenantMdataListService.importHddaMDataList(dataLists,groupId,type);
        return AjaxResult.success();
    }

    /**
     * 导出元数据方案明细模板
     * @param response
     * @throws IOException
     */
    @Log(title = "导出元数据方案明细模板", businessType = BusinessType.EXPORT)
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<TenantMdataList> util = new ExcelUtil<TenantMdataList>(TenantMdataList.class);
        util.importTemplateExcel(response, "元数据明细");
    }
    /**
     * 查询元数据明细列表
     */
    //@PreAuthorize("@ss.hasPermi('manage:YqdaMdataList:list')")
    @Log(title = "查询三合一元数据明细列表", businessType = BusinessType.SELECT)
    @PostMapping("/listforshy")
    public TableDataInfo listforshy(@RequestBody JSONObject json)
    {
        startPage();
        Long groupId=json.getLong("groupId");
        Integer type=json.getInteger("type");
        String fieldCaption=json.getString("fieldCaption");
        String fieldName=json.getString("fieldName");
        Long categoryId= json.getLong("categoryId");
        List<Map<String,Object>> list = tenantMdataListService.selectYqdaMdataListListforshy(groupId,type,fieldCaption,fieldName,categoryId);
        return getDataTable(list);
    }


    @Log(title = "添加三合一元数据明细", businessType = BusinessType.UPDATE)
    @PostMapping("/addmdatatoshy")
    public AjaxResult addmdatatoshy(@RequestBody JSONObject json)
    {
        JSONArray ids=json.getJSONArray("ids");
        Integer type=json.getInteger("type");
        Long categoryId= json.getLong("categoryId");
        Long groupId=json.getLong("groupId");
        Integer count = tenantMdataListService.addmdatatoshy(ids,type,categoryId,groupId);
        return AjaxResult.success(count);
    }
    @Log(title = "删除三合一元数据明细", businessType = BusinessType.DELETE)
    @PostMapping("/deletemdatafromshy")
    public AjaxResult deletemdatafromshy(@RequestBody JSONObject json)
    {
        JSONArray ids=json.getJSONArray("ids");
        Integer type=json.getInteger("type");
        Long categoryId= json.getLong("categoryId");
        Long groupId=json.getLong("groupId");
        Integer count = tenantMdataListService.deletemdatafromshy(ids,type,categoryId,groupId);
        return AjaxResult.success(count);
    }
}
